// 常用图片后缀
const imgSuffix = ['jpg', 'jpeg', 'png', 'gif'];
// 常用视频后缀
const videoSuffix = ["avi", "mov", "rmvb", "rm", "flv", "mp4", "3gp"];

export const base64Fomrat = (base64, imgType = 'png') => {
	return `data:image/${imgType};base64,${base64}`
};
export const waterfallDataFormat = ({
	list = [],
	column = 2
}) => {
	let columnDatas = [];
	let realColumn = column;
	if (column === 1) {
		columnDatas.push(list);
	} else {
		for (let i = 0; i < column; i++) {
			columnDatas.push([]);
			let datas = [];
			list.forEach((m, j) => {
				if (j === i || (j - i) % column === 0) {
					datas.push(m);
				}
			});
			columnDatas[i] = datas;
		}
	}

	columnDatas = columnDatas.filter(m => m.length > 0);
	realColumn = columnDatas.length;
	return {
		datas: columnDatas,
		col: realColumn
	}
};
export const getFileType = (path) => {
	let suffix = 'other';
	try {
		let chars = path.split('.');
		suffix = chars[chars.length - 1];
	} catch (e) {}
	if (imgSuffix.includes(suffix.toLowerCase())) return 'image'
	if (videoSuffix.includes(suffix.toLowerCase())) return 'video'
	return 'file'
};
export const encCardNum = (num) => {
	if (uni.$u.test.empty(num)) return num;
	let pat = /(\d{4})\d*(\d{4})/
	let encnum = num.replace(pat, '$1****$2');
	return encnum;
};
// 驼峰转下划线
export const toUnderLine = (str) => {
	let nstr = str.replace(/[A-Z]/g, function($0) { //函数里只有一个参数时表示与 regexp 中的表达式相匹配的文本。有多个参数时表示与 regexp 中的子表达式相匹配的文本
		return "_" + $0.toLocaleLowerCase();
	});
	//防止有开头大驼峰
	if (nstr.slice(0, 1) == "_") {
		nstr = nstr.slice(1)
	}
	return nstr;
}
// 下划线转驼峰
export const toHump = (str) => {
	// 匹配一个或多个下划线+ 后一个不是以下划线开头的字符
	let nstr = str.replace(/(?:_)+([^_])/g, function($0, $1) {
		return $1.toUpperCase();
	});
	nstr = nstr.replace(nstr[0], nstr[0].toLowerCase());
	return nstr;
}

export const formatTimeStr = (dateTime) => {
	if (!dateTime) return dateTime;
	return dateTime.replace(/-/g, '/').replace(/.000/g, '').replace(/T/g, ' ');
}